﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Shared.SwaggerHelper
{
    /// <summary>
    /// 
    /// </summary>
    public class SwaggerOperationFilter : IOperationFilter
    {
        /// <summary>
        /// Swagger 过滤器
        /// 用于自动填入 WebApi版本信息
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="schemaRegistry"></param>
        /// <param name="apiDescription"></param>
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            foreach (var parameter in operation.Parameters)
            {
                var description = context.ApiDescription.ParameterDescriptions.First(t => "version".Equals(t.Name, StringComparison.OrdinalIgnoreCase));
                if (null != description)
                {
                    var groupName = new Microsoft.OpenApi.Any.OpenApiString(context.ApiDescription
                                                                                   .GroupName
                                                                                   .Replace("v", "", StringComparison.OrdinalIgnoreCase));
                    parameter.Description = $"填写版本号信息:{groupName.Value}";
                    parameter.Schema.Default = groupName;
                }
            }
        }
    }
}
